package com.jcl.recruit.console.mapper.provider;

import com.jcl.recruit.console.model.ConsoleUserAccess;
import com.jcl.recruit.mapper.provider.SqlProvider;
import org.springframework.util.StringUtils;

import java.util.Date;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * @ClassName ConsoleCorpInfoProvider

 * @Author yyliu
 * @Date 2018/12/13 16:54
 * @Version 1.0
 **/
public class ConsoleUserAccessProvider implements SqlProvider<ConsoleUserAccess> {

    private static final String TABLE_NAME = "t_user_access";

    public String insert(ConsoleUserAccess entity) {
        BEGIN();
        INSERT_INTO(TABLE_NAME);
        if(null != entity.getUserId()){
            VALUES("user_id", "#{userId,jdbcType=INTEGER}");
        }

        if(StringUtils.hasLength(entity.getCorpCode())){
            VALUES("corp_code", "#{corpCode,jdbcType=VARCHAR}");
        }
        if(null != entity.getExternalKey()){
            VALUES("external_key", "#{externalKey,jdbcType=VARCHAR}");
        }
        if(null != entity.getExternalUnionKey()){
            VALUES("external_union_key", "#{externalUnionKey,jdbcType=VARCHAR}");
        }
        if(null != entity.getNickname()){
            VALUES("nickname", "#{nickname,jdbcType=VARCHAR}");
        }
        if(null != entity.getUsername()){
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        if(null != entity.getPassword()){
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        if(null != entity.getStatus()){
            VALUES("status", "#{status,jdbcType=INTEGER}");
        }
        if(null != entity.getCategory()){
            VALUES("category", "#{category,jdbcType=INTEGER}");
        }
        if(null != entity.getCreateTime()){
            VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }
        if(null != entity.getUpdateTime()){
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }

        return SQL();
    }

    public String updateByPrimaryKey(ConsoleUserAccess entity) {
        BEGIN();
        UPDATE(TABLE_NAME);

        if(null != entity.getUserId()){
            SET("user_id=#{userId,jdbcType=INTEGER}");
        }
        if(StringUtils.hasLength(entity.getCorpCode())){
            SET("corp_code=#{corpCode,jdbcType=VARCHAR}");
        }
        if(null != entity.getExternalKey()){
            SET("external_key=#{externalKey,jdbcType=VARCHAR}");
        }
        if(null != entity.getExternalUnionKey()){
            SET("external_union_key=#{externalUnionKey,jdbcType=VARCHAR}");
        }
        if(null != entity.getNickname()){
            SET("nickname=#{nickname,jdbcType=VARCHAR}");
        }
        if(null != entity.getUsername()){
            SET("username=#{username,jdbcType=VARCHAR}");
        }
        if(null != entity.getPassword()){
            SET("password=#{password,jdbcType=VARCHAR}");
        }
        if(null != entity.getStatus()){
            SET("status=#{status,jdbcType=INTEGER}");
        }
        if(null != entity.getCategory()){
            SET("category=#{category,jdbcType=INTEGER}");
        }
        entity.setUpdateTime(new Date());
        if(null != entity.getUpdateTime()){
            SET("update_time=#{updateTime,jdbcType=TIMESTAMP}");
        }

        WHERE("id = #{id,jdbcType=INTEGER}");

        return SQL();
    }

}
